<template>
  <u-page class="page-login" :is-nav-bar="false">
    <view class="title-page">Login</view>
    <view>
      <view class="uni-form-item uni-column">
        <view class="input-title">账号</view>
        <input class="uni-input" placeholder="请输入手机号" v-model="loginMap.phone" type="text" />
      </view>
      <view class="uni-form-item uni-column">
        <view class="input-title">密码</view>
        <input class="uni-input" placeholder="请输入密码"  v-model="loginMap.password" type="password" />
      </view>
    </view>
    <button type="primary" @click="loginMap.handleLogin">登录</button>
  </u-page>
</template>
<script lang="ts">
export default {
  name: 'loginPage'
}
</script>
<script lang="ts" setup>
import { reactive } from 'vue'
import $ajax from '@/utils/ajax'
import { setStorage } from '@/utils/common'
import { ResponseType } from '@/schema/common'
import { useUserStore } from '@/store/user'
// 登录
const GET_LOGIN_CGI = '/demo/user/login'
const userStore = useUserStore()
const loginMap = reactive({
  phone: 'admin',
  password: '123456',
  async handleLogin() {
    const res: ResponseType<Record<string, string>> = await $ajax.post({
      url: GET_LOGIN_CGI,
      data: { phone: loginMap.phone, password: loginMap.password }
    })
    if (res.success) {
      userStore.accessToken = res.data.accessToken
      userStore.refreshToken = res.data.refreshToken
      setStorage('userInfo', res.data.userInfo)
      uni.reLaunch({
        url: '/pages/home/index'
      })
    }
  }
})
</script>

<style lang="scss">

.page-login{
  margin: 0 auto;
  padding: 0 20px;
  background: var(--bg-color);
  border-radius: 10px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  .title-page {
    text-align: center;
    color: var(--main-color);
    margin-bottom: 160px;
    padding-top: 60px;
    font-size: 24px;
  }
  .input-title {
    margin-bottom: 14px;
    font-size: 16px;
  }
  .uni-input {
    padding: 10px;
    margin-bottom: 40px;
    border: none;
    border-radius: 5px;
    font-size: 14px;
    box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
  }

  button {
    width: 100%;
    margin-top: 100px;
    background: var(--main-color);
    border: none;
    border-radius: 5px;
    color: #fff;
    cursor: pointer;
  }
}
</style>
